<?php
    /**
    * 中华联合保险接口
    */
    namespace Assist\Controller;
    use Think\Log;

    class CicController extends AssistController {
        public function Api(){
            $raw_post_data = file_get_contents('php://input', 'r');
            
            $retXml = simplexml_load_string($raw_post_data);
            if($retXml === false){
                $this->xmlReturn('0001','XML文件格式解析失败！');
            }else{
                $ret = $this->xmlToArray($raw_post_data);
                if($ret['MAIN']['RESULTCODE']=='0000'){
                    if($ret['MAIN']['TRANSRNO']=='1039'){   //支付通知
                        $serialdecimal = $ret['MAIN']['SERIALDECIMAL'];
                        $transrdate = $ret['MAIN']['TRANSRDATE'];
                        $channelcode = $ret['MAIN']['CHANNELCODE'];
                        $resultcode = $ret['MAIN']['RESULTCODE'];
                        $c_order_no = $ret['BASE']['C_ORDER_NO'];
                        
                        $c_pay_transno = $ret['PAY_INFO']['C_PAY_TRANSNO'];
                        $c_pay_amt = $ret['PAY_INFO']['C_PAY_AMT'];
                        $c_pay_staus = $ret['PAY_INFO']['C_PAY_STAUS'];
                        
                        unset($map);
                        $map['c_order_no'] = $c_order_no;
                        
                        $modelCic = M('CicOrder');
                        $modelCicCash = M('CicOrderCashlog');
                        $modelOrder = M('Order');
                        $resCic = $modelCic->where($map)->find();
                        if($resCic){
                            //先添加支付记录
                            unset($data);
                            $data['corder_id'] = $resCic['corder_id'];
                            $data['serialdecimal'] = $serialdecimal;
                            $data['transrdate'] = $transrdate;
                            $data['channelcode'] = $channelcode;
                            $data['resultcode'] = $resultcode;
                            $data['c_order_no'] = $c_order_no;
                            $data['c_pay_transno'] = $c_pay_transno;
                            $data['c_pay_amt'] = $c_pay_amt;
                            $data['c_pay_staus'] = $c_pay_staus;
                            $modelCicCash->add($data);
                            
                            if($c_pay_staus == '0000'){//支付成功
                                unset($data);
                                $data['status'] = 1; //已支付成功
                                
                                //更新中华保险保单
                                $modelCic->where('corder_id='.$resCic['corder_id'])->save($data);
                                //更新订单状态
                                unset($map);
                                $map['product_id'] = 1;
                                $map['targetid'] = $resCic['corder_id'];
                                $modelOrder->where($map)->save($data);
                            }else{
                                //TODO:支付失败要记录么？                                
                            }
                        }
                        $this->xmlReturn('0000','交易成功',$serialdecimal,$transrdate,$business_code);
                    }else if($ret['MAIN']['TRANSRNO']=='1040'){//保单通知
                        $serialdecimal = $ret['MAIN']['SERIALDECIMAL'];
                        $transrdate = $ret['MAIN']['TRANSRDATE'];
                        $channelcode = $ret['MAIN']['CHANNELCODE'];
                        $resultcode = $ret['MAIN']['RESULTCODE'];
                        $c_order_no = $ret['BASE']['C_ORDER_NO'];
                        $c_staus = $ret['BASE']['C_STAUS'];
                        $c_message = $ret['BASE']['C_MESSAGE'];
                        unset($map);
                        $map['c_order_no'] = $c_order_no;
                        
                        $modelCic = M('CicOrder');
                        $modelCicPolicy = M('CicOrderPolicy'); 
                        $modelCicPolicyList = M('CicOrderPolicyList');
                        $modelOrder = M('Order');
                        $resCic = $modelCic->where($map)->find();
                        if($resCic){
                            //先添加支付记录
                            unset($data);
                            $data['corder_id'] = $resCic['corder_id'];
                            $data['serialdecimal'] = $serialdecimal;
                            $data['transrdate'] = $transrdate;
                            $data['channelcode'] = $channelcode;
                            $data['resultcode'] = $resultcode;
                            $data['c_order_no'] = $c_order_no;
                            $data['c_staus'] = $c_staus;
                            $data['c_message'] = $c_message;
                            $modelCicPolicy->add($data);
                            foreach($ret['POLICY_LIST']['POLICY'] as $policy){
                                unset($data);
                                $data['c_order_id'] = $resCic['corder_id'];
                                $data['c_policy_no'] = $policy['C_POLICY_NO'];
                                $data['business_code'] = $policy['BUSINESS_CODE'];
                                $modelCicPolicyList->add($data);
                            }
                            
                            if($c_staus == '0000'){//保单成功
                                unset($data);
                                $data['status'] = 2; //保单出单成功
                                
                                //更新中华保险保单
                                $modelCic->where('corder_id='.$resCic['corder_id'])->save($data);
                                //更新订单状态
                                unset($map);
                                $map['product_id'] = 1;
                                $map['targetid'] = $resCic['corder_id'];
                                $modelOrder->where($map)->save($data);                        
                            }
                        }
                        
                        $this->xmlReturn('0000','交易成功',$serialdecimal,$transrdate,$business_code);
                    }
                }
                $this->xmlReturn('0000','交易成功');
            }
        }

        protected function xmlReturn($resultcode='',$err_info='',$serialdecimal='',$transrdate='',$bussiness_code='13'){
            if(empty($serialdecimal)){
                $serialdecimal = get_serial_no();
            }
            if(empty($transrdate)){
                $transrdate = date('Y-m-d H:i:s');
            }

            $this->assign('resultcode',$resultcode);
            $this->assign('err_info',$err_info);
            $this->assign('serialdecimal',$serialdecimal);
            $this->assign('transrdate',$transrdate);
            $this->assign('bussiness_code',$bussiness_code);
            $this->display('Cic/CashRet','utf-8','text/xml');
        }



        protected function xmlNameSpaceToArray(\SimpleXmlIterator $xml, $nameSpaces=Null){
            $output = Null;
            $preparedArray = array();
            for($xml->rewind(); $xml->valid(); $xml->next()) {
                $key = $xml->key();
                if(!isset($preparedArray[$key])) { $preparedArray[$key] = array(); $i=0; }
                else $i = count($preparedArray[$key]);
                $simple = true;
                foreach($xml->current()->attributes() as $k=>$v) {
                    $preparedArray[$key][$i][$k]=(string)$v;
                    $simple = false;
                }
                if($nameSpaces) foreach($nameSpaces as $nid=>$name) {
                    foreach($xml->current()->attributes($name) as $k=>$v) {
                        $preparedArray[$key][$i][$nid.':'.$k]=(string)$v;
                        $simple = false;
                    }
                } 
                if($xml->hasChildren()) {
                    if($simple) $preparedArray[$key][$i] = $this->xmlNameSpaceToArray($xml->current(), $nameSpaces);
                    else $preparedArray[$key][$i]['content'] = $this->xmlNameSpaceToArray($xml->current(), $nameSpaces);
                } else {
                    if($simple) $preparedArray[$key][$i] = strval($xml->current());
                    else $preparedArray[$key][$i]['content'] = strval($xml->current());
                }
                $i++;
            }
            $output = $preparedArray;
            return $preparedArray;
        }

        protected function simpleArray(&$array){
            if(is_array($array)){
                if(count($array)==1){//只有一个元素
                    if(isset($array[0])){
                        $array = $array[0];
                        $this->simpleArray($array);
                    }else{
                        foreach($array as &$item){
                            $this->simpleArray($item);
                        }
                    }
                }else{
                    foreach($array as &$item){
                        $this->simpleArray($item);
                    }
                }
            }
        }

        protected function xmlToArray($xmldata){
            $xml = new \SimpleXmlIterator($xmldata);
            $nameSpaces = $xml->getNamespaces(true);
            $output = $this->xmlNameSpaceToArray($xml,$nameSpaces);
            $this->simpleArray($output);
            return $output;
        }

    }

